import * as UmfDialog from '../../../../utils/UmfDialog.js';
import * as network_utils from '../../../../http/NetworkUtils';
import * as constants from '../../../../common/Constants';
import * as StateUtils from '../../../../model/StateUtils';

import { $Toast } from '../../../../components/CompRouter';

Page({

    data: {},

    onLoad: function (options) {
        this.setData({
            userId: options.userId,
            salesmanState: new StateUtils.salemanState(options.salesmanStatus)
        });
    },

    /**
     * 重置登录密码
     */
    tapResetPwd: function () {
        UmfDialog.show("是否重置业务员登录密码为111111", ()=> {
            this.requestResetSalemanPwd();
        }, true);
    },

    /**
     * 启用停用业务员
     */
    startOrStop: function () {
        UmfDialog.show(this.data.salesmanState.alertMsg, () => {
            this._changeStatus()
        }, true)
    },

    requestResetSalemanPwd() {
        let params = {
            userId: this.data.userId
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_RESET_PWD, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    $Toast.showText("业务员登录密码已重置为111111");
                    return true;
                }
                return false;
            }

        }).send();
    },

    _changeStatus: function () {
        let params = {
            userId: this.data.userId,
            salesmanStatus: this.data.salesmanState.sideState
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_CHANGE_SALEMAN_STATUS, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    UmfDialog.show(this.data.salesmanState.completeMsg);
                    if (response.salesmanStatus == '0') {
                        //停用状态返回的是0
                        response.salesmanStatus = '4';
                    }
                    this.setData({
                        salesmanState: new StateUtils.salemanState(response.salesmanStatus)
                    });
                    return true
                }
                return false
            }

        }).send();
    }

});